package com.tiantian.quanminxiu.utils.gift;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.model.GiftModel;
import com.tiantian.quanminxiu.model.RunwayModel;
import com.tiantian.quanminxiu.utils.APPUtils;

import java.lang.ref.WeakReference;
import java.util.concurrent.LinkedBlockingQueue;

public class RunwayShowManager {
    private LinkedBlockingQueue<RunwayModel> queue;//礼物的队列
    private RelativeLayout giftCon;//跑道的容器
    private Activity cxt;//上下文
    private ImageView img;//礼物显示
    private LinkedBlockingQueue<RunwayModel> giftQueue;//大礼物队列

    private final static int SHOW_GIFT_FLAG = 1;//显示礼物
    private final static int GET_QUEUE_GIFT = 0;//从队列中获取礼物
    private final static int REMOVE_GIFT_VIEW = 2;//当礼物的View显示超时，删除礼物View
    private final static int SHOW_GIFT_ANIM = 3;//大礼物gif

    private giftHandler handler = new giftHandler(cxt) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOW_GIFT_FLAG://如果是处理显示礼物的消息
                    RunwayModel showVo = (RunwayModel) msg.obj;
                    String ShowId = showVo.getRunwayId();
                    View giftView = null;
                    //获取礼物的View的布局
                    switch (showVo.getType()){
                        case 1://全服跑道礼物消息
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.runway_servicegiftmessage, null);
                            View linGiftContent = giftView.findViewById(R.id.linGiftContent);
                            ImageView imgUserLevel = (ImageView) giftView.findViewById(R.id.imgUserLevel);
                            TextView txtUserConent = (TextView) giftView.findViewById(R.id.txtUserConent);
                            ImageView imgGiftType = (ImageView) giftView.findViewById(R.id.imgGiftType);
                            imgUserLevel.setImageResource(APPUtils.getLevelPic(cxt,showVo.getUserlevel()));
                            GiftModel giftModel = showVo.getGiftModel();
                            APPUtils.displayImage(APPUtils.createGiftImg(giftModel.getGiftID()),imgGiftType);
                            txtUserConent.setText(Html.fromHtml("<font color='#ffff83'>"+giftModel.getSendName()+"</font> 赠送 " +"<font color='#ffff83'>"+giftModel.getAnchorName()+"</font> "+giftModel.getGiftName()+"x"+giftModel.getNum()));
                            Animation inAnim = (TranslateAnimation) AnimationUtils.loadAnimation(cxt, R.anim.runway_in);
                            giftView.startAnimation(inAnim);//播放礼物View出现的动
                            final View finalGiftView = giftView;
                            inAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    Message msg = new Message();
                                    msg.obj = finalGiftView;
                                    msg.what = REMOVE_GIFT_VIEW;
                                    handler.sendMessage(msg);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            });
                            break;
                        case 2://飞屏
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.runway_roommessage, null);
                            TextView txtHorn = (TextView) giftView.findViewById(R.id.txtUserContent);
                            txtHorn.setText(Html.fromHtml("<font color='#ffff83'>"+showVo.getUserName()+"：</font>" + showVo.getContent()));
                            Animation inWaitAnim = (TranslateAnimation) AnimationUtils.loadAnimation(cxt, R.anim.runway_in_wait);
                            giftView.startAnimation(inWaitAnim);//播放礼物View出现的动
                            final View finalView = giftView;
                            inWaitAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    Message msg = new Message();
                                    msg.obj = finalView;
                                    msg.what = REMOVE_GIFT_VIEW;
                                    handler.sendMessageDelayed(msg,5000);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            } );
                            break;
                        case 3://喇叭
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.runway_servicemessage, null);
                            ImageView imgUserAvatar = (ImageView) giftView.findViewById(R.id.imgUserAvatar);
                            TextView txtUserContent = (TextView) giftView.findViewById(R.id.txtUserContent);
                            TextView txtUserName = (TextView) giftView.findViewById(R.id.txtUserName);
                            TextView txtAnchorName = (TextView) giftView.findViewById(R.id.txtAnchorName);
                            txtUserContent.setText(showVo.getContent());
                            txtUserName.setText(showVo.getUserName());
                            txtAnchorName.setText("("+showVo.getAnchorName()+" 直播间)");
                            imgUserAvatar.post(new Runnable() {
                                @Override
                                public void run() {
                                    APPUtils.displayImage(APPUtils.createUserAvatar(showVo.getUserId()), imgUserAvatar, cxt, imgUserAvatar.getWidth() / 2);
                                }
                            });
                            Animation inMsgAnim = (TranslateAnimation) AnimationUtils.loadAnimation(cxt, R.anim.runway_in_wait);
                            giftView.startAnimation(inMsgAnim);//播放礼物View出现的动
                            final View finalMsgView = giftView;
                            inMsgAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    Message msg = new Message();
                                    msg.obj = finalMsgView;
                                    msg.what = REMOVE_GIFT_VIEW;
                                    handler.sendMessageDelayed(msg,5000);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            } );
                            break;
                        case 4://全服跑道守护消息
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.runway_servicegiftmessage, null);
                            View linGuardContent = giftView.findViewById(R.id.linGiftContent);
                            TextView txtGuardUserConent = (TextView) giftView.findViewById(R.id.txtUserConent);
                            txtGuardUserConent.setText(Html.fromHtml("恭喜 " + "[level"+showVo.getUserlevel()+"] " + "<font color='#ffff83'>"+showVo.getUserName()+"</font>" +
                                    " 成为" + " [anchorlevel"+showVo.getAnchorlevel()+"] " +"<font color='#ffff83'>"+showVo.getAnchorName()+"</font>" +" 守护"));
                            Animation inGuardAnim = (TranslateAnimation) AnimationUtils.loadAnimation(cxt, R.anim.runway_in);
                            giftView.startAnimation(inGuardAnim);//播放礼物View出现的动
                            final View finalguradtView = giftView;
                            inGuardAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    Message msg = new Message();
                                    msg.obj = finalguradtView;
                                    msg.what = REMOVE_GIFT_VIEW;
                                    handler.sendMessage(msg);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            });
                            break;
                        case 5://用户进入房间消息
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.userjoin_message, null);
                            TextView txtNotify = (TextView) giftView.findViewById(R.id.txtNotify);
                            txtNotify.setText(Html.fromHtml("[level"+showVo.getUserlevel()+"] " +"<font color='#ffff83'>"+showVo.getUserName()+"</font>" +" 进入直播间"));
                            Animation userJoinAnim = (TranslateAnimation) AnimationUtils.loadAnimation(cxt, R.anim.runway_left_in);
                            giftView.startAnimation(userJoinAnim);//播放礼物View出现的动
                            final View userJoinView = giftView;
                            userJoinAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    Message msg = new Message();
                                    msg.obj = userJoinView;
                                    msg.what = REMOVE_GIFT_VIEW;
                                    handler.sendMessageDelayed(msg,2000);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            });
                            break;
                    }

                    giftView.setTag(ShowId);
                    //将礼物的View添加到礼物的ViewGroup中
                    giftCon.addView(giftView);
                    break;
                case GET_QUEUE_GIFT://如果是从队列中获取礼物实体的消息
                    int size = queue.size();
                    if (size > 0&&(giftCon.getChildCount()==0)) {//如果从队列中获取的礼物不为空，那么就将礼物展示在界面上
                        RunwayModel vo = queue.poll();
                        Message giftMsg = new Message();
                        giftMsg.obj = vo;
                        giftMsg.what = SHOW_GIFT_FLAG;
                        handler.sendMessage(giftMsg);
                    } else {
                        handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//如果这次从队列中获取的消息是礼物是空的，则一秒之后重新获取
                    }
                    break;

                case REMOVE_GIFT_VIEW:
                    View removeView = (View) msg.obj;
                    giftCon.removeView(removeView);
                    break;
                case SHOW_GIFT_ANIM:
                    int giftSize = giftQueue.size();
                    if (giftSize>0){
                        //img 显示大礼物动画

                    }else {
                        handler.sendEmptyMessageDelayed(SHOW_GIFT_ANIM,1000);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public RunwayShowManager(Activity cxt, final RelativeLayout giftCon) {
        this.cxt = cxt;
        this.giftCon = giftCon;
        queue = new LinkedBlockingQueue<RunwayModel>();
    }

    public RunwayShowManager(Activity cxt,final ImageView img){
        this.cxt = cxt;
        this.img = img;
        giftQueue = new LinkedBlockingQueue<RunwayModel>();
    }

    public void showGiftAnim(){
        handler.sendEmptyMessageDelayed(SHOW_GIFT_ANIM,1000);
    }

    //开始显示礼物
    public void showRunWay() {
        handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//轮询队列获取礼物
    }

    //放入礼物到队列
    public boolean addRunWay(RunwayModel vo) {
        return queue.add(vo);
    }

    public boolean addGiftWay(RunwayModel rm){
        return giftQueue.add(rm);
    }

    //释放内存
    public void leakHandle(){
        handler.removeCallbacksAndMessages(null);
        cxt = null;
    }

    private static class giftHandler extends Handler {
        private final WeakReference<Activity> mActivity;

        public giftHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mActivity.get() == null) {
                return;
            }
        }
    }

}
